package cn.edu.sctu.sctu_lysd.view.act;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Required;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.databinding.ActivityHarvestAddressBinding;

/**
 * Created by wangfuxin on 2017-04-14.
 */

public class HarvestAddressAct extends BaseActivity implements Validator.ValidationListener{

    private ActivityHarvestAddressBinding mBinding;

    private String destinationPre;
    private String destinationDetail;
    private String receiverName;
    private String receiverPhone;

    public final static String DESTINATION_PRE = "destinationPre";
    public final static String DESTINATION_DETAIL = "destinationDetail";
    public final static String RECEIVER_NAME = "receiverName";
    public final static String RECEIVER_PHONE = "receiverPhone";

    @Required(order = 1, message = "必填选项！")
    private EditText receiverNameEdit;

    @Required(order = 2, message = "必填选项！")
    private EditText receiverPhoneEdit;

    @Required(order = 3, message = "必填选项！")
    private EditText destinationDetailEdit;

    private TextView destinationPreTxt;

    private Validator validator;

    private IndexPlacePopWin placePopWin;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_harvest_address;
    }

    @Override
    protected void initAfter(Object bind) {

        mBinding = (ActivityHarvestAddressBinding) bind;

        validator = new Validator(this);

        validator.setValidationListener(this);

        mBinding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        receiverNameEdit = mBinding.receiverName;
        receiverPhoneEdit = mBinding.receiverPhone;
        destinationPreTxt = mBinding.receiverDestinationPre;
        destinationDetailEdit = mBinding.receiverDestinationDetail;


        mBinding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                validator.validate();

            }
        });

        mBinding.receiverDestinationPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                showPopWin(mBinding.receiverDestinationPre);

            }
        });


    }

    private void showPopWin(final View view){

        lightOff();

        View contentView = LayoutInflater.from(this).inflate(R.layout.pup_win_place, null);

        placePopWin = new IndexPlacePopWin(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true,
                this,view);

        ImageView back =  (ImageView) contentView.findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                placePopWin.dismiss();
            }
        });

        placePopWin.setSelectOver(new IndexPlacePopWin.selectOver() {
            @Override
            public void selectOver(String place) {

                /**
                 * 通知 View 显示
                 */

                mBinding.receiverDestinationPre.setText(place);

                placePopWin.dismiss();

            }
        });

        placePopWin.showAsDropDown();

        placePopWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

                lightOn();
            }
        });

    }

    /**
     * 内容区域变亮
     */
    private void lightOn() {
        final WindowManager.LayoutParams lp = this.getWindow().getAttributes();

        /**
         * 值动画
         */
        ValueAnimator valueAnimator = new ValueAnimator().ofFloat(0.3f, 1.0f);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                lp.alpha = (float) animation.getAnimatedValue();
                HarvestAddressAct.this.getWindow().setAttributes(lp);
            }
        });

        valueAnimator.start();
    }

    /**
     * 内容区域变暗
     */
    private void lightOff() {

        final WindowManager.LayoutParams lp = this.getWindow().getAttributes();

        /**
         * 值动画
         */
        ValueAnimator valueAnimator = new ValueAnimator().ofFloat(1.0f, 0.3f);
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                lp.alpha = (float) animation.getAnimatedValue();
                HarvestAddressAct.this.getWindow().setAttributes(lp);
            }
        });

        valueAnimator.start();

        this.getWindow().setAttributes(lp);
    }


    private String getInputSting(TextView view) {
        return view.getText().toString();
    }


    @Override
    public void onValidationSucceeded() {

        receiverName = getInputSting(receiverNameEdit);
        receiverPhone = getInputSting(receiverPhoneEdit);
        destinationPre = getInputSting(destinationPreTxt);
        destinationDetail = getInputSting(destinationDetailEdit);

        Intent intent = new Intent(this,SubmitReleaseAct.class);

        Bundle bundle = new Bundle();

        bundle.putString(RECEIVER_NAME,receiverName);
        bundle.putString(RECEIVER_PHONE,receiverPhone);
        bundle.putString(DESTINATION_PRE,destinationPre);
        bundle.putString(DESTINATION_DETAIL,destinationDetail);

        intent.putExtras(bundle);

        setResult(RESULT_OK,intent);

        finish();

    }

    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {


        String message = failedRule.getFailureMessage();

        if (failedView instanceof EditText) {
            failedView.requestFocus();
            ((EditText) failedView).setError(message);
        } else {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }


    }
}
